package cn.juque.jdfsystem.listener;

import cn.hutool.core.util.ClassUtil;
import cn.juque.jdfsystem.cache.BlacklistInfoCache;
import cn.juque.jdfsystem.entity.BlacklistInfo;
import cn.juque.lucenecandy.core.datasync.IDataSyncService;
import cn.juque.lucenecandy.core.datasync.listener.ITccControllerListener;
import cn.juque.lucenecandy.vo.IndexUpdateParamVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>其他节点黑名单发生变更同步后，刷新本地缓存</li>
 * </ul>
 * @date 2024-07-07
 **/
@Component("blacklistTccControllerListener")
public class BlacklistTccControllerListener implements ITccControllerListener {

    @Resource
    private BlacklistInfoCache blacklistInfoCache;

    @Override
    public Boolean before(IDataSyncService.SyncTypeEnum syncTypeEnum, IndexUpdateParamVO param) {
        return true;
    }

    @Override
    public void after(IDataSyncService.SyncTypeEnum syncTypeEnum, IndexUpdateParamVO param) {
        if (!ClassUtil.equals(BlacklistInfo.class, param.getClassName(), false)) {
            return;
        }
        if (!IDataSyncService.SyncTypeEnum.COMMIT.equals(syncTypeEnum)) {
            return;
        }
        this.blacklistInfoCache.refresh();
    }
}
